//
//  ForgetPSWViewController.m
//  陶瓷
//
//  Created by 郭天龙 on 15/9/7.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "ForgetPSWViewController.h"

@interface ForgetPSWViewController ()

@property (nonatomic, weak) IBOutlet UITextField *phoneNumTF;
@property (nonatomic, weak) IBOutlet UITextField *verifyCodeTF;
@property (nonatomic, weak) IBOutlet UITextField *anewPasswordTF;
@property (nonatomic, weak) IBOutlet UITextField *confirmPasswordTF;
@property (nonatomic, weak) IBOutlet UIButton *verifyCodeButton;
@property (nonatomic, weak) IBOutlet UIButton *resetPasswordBtn;

@property (nonatomic, weak) IBOutlet UIView *background;
@property (nonatomic, strong) UIButton *cover;
@property (nonatomic, strong) NSTimer *countDownTimer;
@property (nonatomic, assign) int stopTime;
@end

@implementation ForgetPSWViewController

- (void)showCurrentRemainSecond
{
    int nowSince = [[NSDate date] timeIntervalSince1970];
    NSString *str = [NSString stringWithFormat:@"重新获取%ds",self.stopTime - nowSince];
    self.verifyCodeButton.backgroundColor = [UIColor lightGrayColor];
    [self.verifyCodeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; ;
    [self.verifyCodeButton setTitle:str forState:UIControlStateNormal];
}
- (void)setCountDownState
{
    self.stopTime = [[NSDate date] timeIntervalSince1970];
    self.stopTime += 90;
    self.verifyCodeButton.enabled = NO;
    [self showCurrentRemainSecond];
    [self stopTimer];
    self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
}
- (void)countDownAction
{
    if ([[NSDate date] timeIntervalSince1970] > self.stopTime) {
        [self resumeVerifyButtonStatus];
    } else {
        [self showCurrentRemainSecond];
    }
}

- (void)resumeVerifyButtonStatus
{
    [self stopTimer];
    self.verifyCodeButton.enabled = YES;
    [self.verifyCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.verifyCodeButton.backgroundColor = [UIColor whiteColor];
    [self.verifyCodeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

- (void)stopTimer
{
    if (self.countDownTimer != nil) {
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
    }
}

- (void)dealloc
{
    [self stopTime];
}

- (UIButton *)cover {
    if (_cover == nil) {
        _cover = [[UIButton alloc] initWithFrame:self.view.bounds];
        _cover.backgroundColor = [UIColor clearColor];
    }
    return _cover;
}

- (void)resignTF
{
    [_phoneNumTF resignFirstResponder];
    [_anewPasswordTF resignFirstResponder];
    [_verifyCodeTF resignFirstResponder];
    [_confirmPasswordTF resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.navigationItem setTitle:@"忘记密码"];
    
    
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignTF)];
    gesture.numberOfTapsRequired = 1;
    [self.background addGestureRecognizer:gesture];
    
    self.anewPasswordTF.secureTextEntry = YES;
    self.confirmPasswordTF.secureTextEntry = YES;
    
    _verifyCodeButton.layer.cornerRadius = 10;
    _verifyCodeButton.layer.borderWidth = 0.5;
    _verifyCodeButton.layer.borderColor = [UIColor blueColor].CGColor;
    [_verifyCodeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.verifyCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    _resetPasswordBtn.layer.cornerRadius = 10;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.cover removeFromSuperview];
    [SVProgressHUD dismiss];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)verifyBtnTouchUpInside:(UIButton *)verifyBtn
{
    if (_phoneNumTF.text.length == 0) {
        [STAlertView st_promptAlertWithMessage:@"请填入手机号"];
        return;
    }
    [SVProgressHUD showWithStatus:@"获取验证码 请稍候"];
    [self.view addSubview:self.cover];
    [self setCountDownState];
    [[TCNetworkTool sharedNetTool] verifyCodeResetWithCallNumber:_phoneNumTF.text andSuccessBlocks:^(NSString *successMessage) {
        [SVProgressHUD dismiss];
        [self.cover removeFromSuperview];
    } andFailureBlocks:^(NSString *failureMessage) {
        [SVProgressHUD dismiss];
        [self.cover removeFromSuperview];
        [MBHUDHelper showWarningWithText:failureMessage];
    }];
}

- (IBAction)resetBtnTouchUpInside:(UIButton *)resetBtn
{
    if (_verifyCodeTF.text.length == 0) {
        [STAlertView st_promptAlertWithMessage:@"请输入验证码"];
        return;
    }
    if (_anewPasswordTF.text.length == 0) {
        [STAlertView st_promptAlertWithMessage:@"请填入新密码"];
        return;
    }
    if (_confirmPasswordTF.text.length == 0) {
        [STAlertView st_promptAlertWithMessage:@"请填入确认密码"];
        return;
    }
    [SVProgressHUD showWithStatus:@"正在修改 请稍候"];
    [self.view addSubview:self.cover];
    [[TCNetworkTool sharedNetTool] setPasswordWithCellPhone:_phoneNumTF.text VerifyCode:_verifyCodeTF.text NewPasswork:_anewPasswordTF.text confirmPassword:_confirmPasswordTF.text andSuccessBlocks:^(NSString *successMessage) {
        [SVProgressHUD dismiss];
        [self.cover removeFromSuperview];
        [self.navigationController popViewControllerAnimated:YES];
    } andFailureBlocks:^(NSString *failureMessage) {
        [SVProgressHUD dismiss];
        [self.cover removeFromSuperview];
        [MBHUDHelper showWarningWithText:failureMessage];
    }];
}

@end
